package com.sophialex.health.record.views;

import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.part.ViewPart;

import com.sophialex.health.common.platform.SuperContext;
import com.sophialex.health.person.vo.HrPerson;
import com.sophialex.health.record.common.util.EventType;
import com.sophialex.health.record.common.util.HealthRecordListenerRegister;
import com.sophialex.health.record.common.utils.CommonUtils;
import com.sophialex.health.record.common.utils.ImageUtil;
import com.sophialex.health.system.frame.resource.SWTResourceManager;
public class PersonTopLeftView extends ViewPart implements IPropertyChangeListener {
	
	private Label label_name;
	
	private Label label_sex;
	
	private Label label_age;
	
	private Label label_address;
	
	public PersonTopLeftView() {
	}
	public static final String ID = "com.sophialex.health.record.views.PersonTopLeftView";
	private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
	
	public void createPartControl(Composite parent) {
		parent.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		ScrolledComposite scrolledComposite = new ScrolledComposite(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		toolkit.adapt(scrolledComposite);
		toolkit.paintBordersFor(scrolledComposite);
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		Form form = toolkit.createForm(scrolledComposite);
		toolkit.paintBordersFor(form);
		form.getBody().setLayout(null);
		
		Label label = toolkit.createLabel(form.getBody(), "", SWT.NONE);
		label.setBackgroundImage(ImageUtil.no_pic);
		label.setBounds(10, 10, 94, 114);
		
		label_name = toolkit.createLabel(form.getBody(), "", SWT.NONE);
		label_name.setFont(SWTResourceManager.getFont("新宋体", 11, SWT.BOLD));
		label_name.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_name.setBounds(118, 26, 68, 22);
		
		label_sex = toolkit.createLabel(form.getBody(), "", SWT.NONE);
		label_sex.setFont(SWTResourceManager.getFont("新宋体", 11, SWT.BOLD));
		label_sex.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_sex.setBounds(118, 66, 54, 22);
		
		label_age = toolkit.createLabel(form.getBody(), "", SWT.NONE);
		label_age.setFont(SWTResourceManager.getFont("新宋体", 11, SWT.BOLD));
		label_age.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_age.setBounds(118, 102, 54, 22);
		
		label_address = toolkit.createLabel(form.getBody(), "", SWT.NONE);
		label_address.setFont(SWTResourceManager.getFont("新宋体", 11, SWT.BOLD));
		label_address.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_address.setBounds(10, 132, 198, 22);
		scrolledComposite.setContent(form);
		scrolledComposite.setMinSize(form.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        setData();
        HealthRecordListenerRegister.getInstance().addPropertyChangeListener(this);
	}
    
	public void setData(){
		HrPerson currentPerson = SuperContext.currentPerson;
		if(currentPerson!=null){
			label_name.setText(currentPerson.getName());
			label_sex.setText(CommonUtils.getValueByNumForDictCode(currentPerson.getSex(),"SEX"));
			label_address.setText(currentPerson.getCadress()==null?"":currentPerson.getCadress());
			label_age.setText(currentPerson.getBirthday()==null?"":CommonUtils.calcAge(currentPerson.getBirthday()));
		}else{
			label_name.setText("");
			label_sex.setText("");
			label_address.setText("");
			label_age.setText("");
		}
	}
	@Override
	public void setFocus() {
	}

	@Override
	public void propertyChange(PropertyChangeEvent event) {
		if(event.getProperty().equals(EventType.REFRESH)){
			setData();
		}
	}
}